const BaseIpcHandler = require('../BaseIpcHandler');
const { RemoteScriptInvoker } = require('../../remote/remoteScriptInvoker');
const { LocalSystemInfo } = require('../../remote/systemInfo');
const { CondaManager } = require('../../remote/condaUtils');

class CondaEnvDetailsHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log(`>> CondaEnvDetailsHandler params = `, params);
        const { remoteId, env } = params;

        // 业务逻辑
        let result = null;
        try {
            let invoker = remoteId == 'local' ? new LocalSystemInfo() : new RemoteScriptInvoker(this.app, remoteId);
            let system_info = await invoker.getSystemInfo();
            let conda_query = new CondaManager(this.app, remoteId, system_info);
            let envlist = await conda_query.getPipPackages(env);
            result = { code: 0, msg: envlist };
        } catch (err) {
            console.error(`Get remote conda details Failed!, Error =`, err);
            result = { code: -1, msg: err.msg };
        }

        // 返回结果
        // console.log(`CondaEnvDetailsHandler result = `, result);
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = CondaEnvDetailsHandler;
